package cn.edu.hpu.jie.services;

import cn.edu.hpu.jie.dao.UserDao;
import cn.edu.hpu.jie.entity.User;
import cn.edu.hpu.jie.util.HmacUtil;

public class UserService {
	private UserDao dao = new UserDao();
	
	/**
	 * 验证用户登录
	 * @param userName
	 * @param clientPassword
	 * @param challenge
	 * @return null 系统出错或验证失败  u 验证成功 
	 */
	public User checkLogin(String userName, String clientPassword, String challenge) {
		User u = getUserByUserName(userName);
		
		if(u != null) {
			 try {
					String serverPassword = HmacUtil.getHmacString(u.getPassword(), challenge);
					if(!clientPassword.equals(serverPassword)) {
						return null;
					}
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
		}
		return u;
	}
	
	/**
	 * 根据用户名获取用户
	 * @param userName
	 * @return
	 */
	public User getUserByUserName(String userName) {
		return dao.getUserByUserName(userName);
	}
	
	/**
	 * 插入新用户
	 * @param u
	 * @return
	 */
	public int insertNewUser(User u) {
		return dao.insertUser(u);
	}
}
